﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiDisAutomaticDisassemblyForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CUiDisAutomaticDisassemblyForm : TxForm
{
  private const string OPERATION_TYPE = "Compound Operation";
  private Container components;
  private Label label2;
  private ComboBox m_cbOperationType;
  private CheckBox m_cbAssembly;
  private CheckBox m_cbDisassembly;
  private RadioButton m_rbAutomatic;
  private RadioButton m_rbExact;
  private Button m_btnGenerate;
  private Button m_btnClose;
  private CApDisAutomaticDisassemblyApp m_manager;
  private NumericUpDown m_nudExactValue;
  private GroupBox groupBox1;
  private GroupBox groupBox2;
  private Label label5;
  private const string RESOURCE_TABLE_PATH = "DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable";
  private const string DISTANCE_MODE_ENTRY_NAME = "DistanceMode";
  private const string DIRECTION_TYPE_ENTRY_NAME = "DirectionType";
  private const string DISTANCE_VALUE_ENTRY_NAME = "DistanceValue";
  private const string OPERATION_DURATION_VALUE_ENTRY_NAME = "OperationDistance";
  private const string OPERATION_TYPE_ENTRY_NAME = "OperationType";
  private const string PRECISION = "Precision";
  private const string CLEARANCE = "Clearance";
  private const string COLLECTION_NAME = "PersistencyData";
  private const string SHORTEST_PATH_COLLECTION_NAME = "ShortestPathSequenceGenerationPersistencyData";
  private TxObjEditBoxCtrl m_disassemblyRoot;
  private CUiDisAutomaticDisassemblyForm.SequenceGenerationMode m_mode;
  private Label m_partLable;
  private TxNumericEditBoxCtrl m_nudOperationDuration;
  private CApShortestPathValidator m_pickValidator;

  public CUiDisAutomaticDisassemblyForm(
    CUiDisAutomaticDisassemblyForm.SequenceGenerationMode mode)
  {
    this.m_mode = mode;
    this.InitializeComponent();
    if (this.m_mode != CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.ShortestPath)
      return;
    this.m_pickValidator = new CApShortestPathValidator();
    this.m_disassemblyRoot.SetValidator((ITxValidator) this.m_pickValidator);
    this.ChangeTitlesToShoretstPathSequenceCommand();
    this.UpdateButtons();
  }

  public void Show(ITxOperation currentSelectedObject)
  {
    this.InitDialog(currentSelectedObject);
    ((Control) this).Show();
  }

  protected virtual void Dispose(bool disposing)
  {
    this.UnRegisterToDocumentUnloadEvent();
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiDisAutomaticDisassemblyForm));
    this.m_partLable = new Label();
    this.label2 = new Label();
    this.m_cbOperationType = new ComboBox();
    this.m_cbAssembly = new CheckBox();
    this.m_cbDisassembly = new CheckBox();
    this.m_nudExactValue = new NumericUpDown();
    this.m_rbExact = new RadioButton();
    this.m_rbAutomatic = new RadioButton();
    this.m_btnGenerate = new Button();
    this.m_btnClose = new Button();
    this.groupBox1 = new GroupBox();
    this.groupBox2 = new GroupBox();
    this.label5 = new Label();
    this.m_disassemblyRoot = new TxObjEditBoxCtrl();
    this.m_nudOperationDuration = new TxNumericEditBoxCtrl();
    this.m_nudExactValue.BeginInit();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_partLable, "m_partLable");
    this.m_partLable.Name = "m_partLable";
    componentResourceManager.ApplyResources((object) this.label2, "label2");
    this.label2.Name = "label2";
    this.m_cbOperationType.DropDownStyle = ComboBoxStyle.DropDownList;
    componentResourceManager.ApplyResources((object) this.m_cbOperationType, "m_cbOperationType");
    this.m_cbOperationType.Name = "m_cbOperationType";
    this.m_cbOperationType.SelectedIndexChanged += new EventHandler(this.m_cbActivityType_SelectedIndexChanged);
    componentResourceManager.ApplyResources((object) this.m_cbAssembly, "m_cbAssembly");
    this.m_cbAssembly.Name = "m_cbAssembly";
    this.m_cbAssembly.CheckedChanged += new EventHandler(this.m_cbAssemblym_CheckedChanged);
    componentResourceManager.ApplyResources((object) this.m_cbDisassembly, "m_cbDisassembly");
    this.m_cbDisassembly.Name = "m_cbDisassembly";
    this.m_cbDisassembly.CheckedChanged += new EventHandler(this.m_cbDisassembly_CheckedChanged);
    componentResourceManager.ApplyResources((object) this.m_nudExactValue, "m_nudExactValue");
    this.m_nudExactValue.Maximum = new Decimal(new int[4]
    {
      10000,
      0,
      0,
      0
    });
    this.m_nudExactValue.Minimum = new Decimal(new int[4]
    {
      1,
      0,
      0,
      0
    });
    this.m_nudExactValue.Name = "m_nudExactValue";
    this.m_nudExactValue.Value = new Decimal(new int[4]
    {
      100,
      0,
      0,
      0
    });
    componentResourceManager.ApplyResources((object) this.m_rbExact, "m_rbExact");
    this.m_rbExact.Name = "m_rbExact";
    this.m_rbExact.CheckedChanged += new EventHandler(this.m_rbExact_CheckedChanged);
    this.m_rbAutomatic.Checked = true;
    componentResourceManager.ApplyResources((object) this.m_rbAutomatic, "m_rbAutomatic");
    this.m_rbAutomatic.Name = "m_rbAutomatic";
    this.m_rbAutomatic.TabStop = true;
    this.m_rbAutomatic.CheckedChanged += new EventHandler(this.m_rbAutomatic_CheckedChanged);
    componentResourceManager.ApplyResources((object) this.m_btnGenerate, "m_btnGenerate");
    this.m_btnGenerate.Name = "m_btnGenerate";
    this.m_btnGenerate.Click += new EventHandler(this.m_btnGenerate_Click);
    this.m_btnClose.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_btnClose, "m_btnClose");
    this.m_btnClose.Name = "m_btnClose";
    this.m_btnClose.Click += new EventHandler(this.m_btnClose_Click);
    this.groupBox1.Controls.Add((Control) this.m_cbDisassembly);
    this.groupBox1.Controls.Add((Control) this.m_cbAssembly);
    componentResourceManager.ApplyResources((object) this.groupBox1, "groupBox1");
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.TabStop = false;
    this.groupBox2.Controls.Add((Control) this.m_rbExact);
    this.groupBox2.Controls.Add((Control) this.m_nudExactValue);
    this.groupBox2.Controls.Add((Control) this.m_rbAutomatic);
    componentResourceManager.ApplyResources((object) this.groupBox2, "groupBox2");
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.TabStop = false;
    componentResourceManager.ApplyResources((object) this.label5, "label5");
    this.label5.Name = "label5";
    this.m_disassemblyRoot.KeepFaceEmphasizedWhenControlIsNotFocused = true;
    this.m_disassemblyRoot.ListenToPick = true;
    componentResourceManager.ApplyResources((object) this.m_disassemblyRoot, "m_disassemblyRoot");
    ((Control) this.m_disassemblyRoot).Name = "m_disassemblyRoot";
    this.m_disassemblyRoot.Object = (ITxObject) null;
    this.m_disassemblyRoot.PickAndClear = false;
    this.m_disassemblyRoot.PickLevel = (TxPickLevel) 1;
    this.m_disassemblyRoot.PickOnly = false;
    this.m_disassemblyRoot.ReadOnly = false;
    this.m_disassemblyRoot.ValidatorType = (TxValidatorType) 19;
    this.m_disassemblyRoot.TypeInvalid += new EventHandler(this.m_disassemblyRoot_TypeInvalid);
    this.m_disassemblyRoot.TypeValid += new EventHandler(this.m_disassemblyRoot_TypeValid);
    this.m_disassemblyRoot.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.m_disassemblyRoot_Picked);
    ((Control) this.m_disassemblyRoot).LostFocus += new EventHandler(this.m_disassemblyRoot_LostFocus);
    this.m_nudOperationDuration.BorderStyle = BorderStyle.Fixed3D;
    componentResourceManager.ApplyResources((object) this.m_nudOperationDuration, "m_nudOperationDuration");
    this.m_nudOperationDuration.LowerBound = 0.01;
    ((Control) this.m_nudOperationDuration).Name = "m_nudOperationDuration";
    this.m_nudOperationDuration.ReadOnly = false;
    this.m_nudOperationDuration.StepSize = 1.0;
    this.m_nudOperationDuration.TextAlign = HorizontalAlignment.Left;
    this.m_nudOperationDuration.ThousandsSeparator = false;
    this.m_nudOperationDuration.UpperBound = 1000000.0;
    this.m_nudOperationDuration.UseDecimalPlacesFromOption = true;
    this.m_nudOperationDuration.Value = 0.01;
    this.m_nudOperationDuration.ValueType = (TxNumericEditValueType) 2;
    ((Form) this).AcceptButton = (IButtonControl) this.m_btnGenerate;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((Form) this).CancelButton = (IButtonControl) this.m_btnClose;
    ((Control) this).Controls.Add((Control) this.m_nudOperationDuration);
    ((Control) this).Controls.Add((Control) this.m_disassemblyRoot);
    ((Control) this).Controls.Add((Control) this.label5);
    ((Control) this).Controls.Add((Control) this.groupBox2);
    ((Control) this).Controls.Add((Control) this.groupBox1);
    ((Control) this).Controls.Add((Control) this.m_btnClose);
    ((Control) this).Controls.Add((Control) this.m_btnGenerate);
    ((Control) this).Controls.Add((Control) this.m_cbOperationType);
    ((Control) this).Controls.Add((Control) this.label2);
    ((Control) this).Controls.Add((Control) this.m_partLable);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedSingle;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiDisAutomaticDisassemblyForm);
    ((Form) this).ShowIcon = false;
    ((Form) this).Closing += new CancelEventHandler(this.CUiDisAutomaticDisassemblyForm_Closing);
    this.m_nudExactValue.EndInit();
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }

  private ResourceManager GetResourceManger()
  {
    return new ResourceManager("DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable", ((object) this).GetType().Assembly);
  }

  private void m_cbAssemblym_CheckedChanged(object sender, EventArgs e)
  {
    if (this.m_cbAssembly.Checked)
    {
      this.m_cbDisassembly.Checked = true;
      this.m_cbDisassembly.Enabled = false;
    }
    else
      this.m_cbDisassembly.Enabled = true;
    this.UpdateButtons();
  }

  private void m_cbDisassembly_CheckedChanged(object sender, EventArgs e) => this.UpdateButtons();

  private void m_rbAutomatic_CheckedChanged(object sender, EventArgs e)
  {
    this.m_nudExactValue.Enabled = false;
  }

  private void m_rbExact_CheckedChanged(object sender, EventArgs e)
  {
    this.m_nudExactValue.Enabled = true;
  }

  private void InitDialog(ITxOperation currentSelectedObject)
  {
    this.RegisterToDocumentUnLoadEvent();
    this.m_manager = new CApDisAutomaticDisassemblyApp(currentSelectedObject);
    if (this.m_mode == CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.Disassembly)
    {
      ((Control) this.m_disassemblyRoot).Text = this.m_manager.GetDisassemblyRootName();
      this.m_disassemblyRoot.ReadOnly = true;
    }
    object[] allowedActivityTypes = this.m_manager.GetAllowedActivityTypes();
    this.m_cbOperationType.Items.Add((object) "Compound Operation");
    if (allowedActivityTypes != null)
    {
      switch (currentSelectedObject)
      {
        case TxTcActivity _:
        case TxTcOperation _:
          this.m_cbOperationType.Items.AddRange(allowedActivityTypes);
          break;
      }
    }
    TxApplication.ApplicationEvents.Exiting += new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
    this.SetConfigurationData(this.LoadDialogConfiguration());
    this.UpdateButtons();
  }

  private void RegisterToDocumentUnLoadEvent()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void UnRegisterToDocumentUnloadEvent()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    ((Form) this).Close();
  }

  private void m_btnGenerate_Click(object sender, EventArgs e)
  {
    if (!this.m_manager.AreGlobalDirectionDefined())
    {
      int num = (int) TxMessageBox.Show(this.GetResourceManger().GetString("AUTOMATIC_DISASSEMBLY_THERE_ARE_NO_GLOBAL_DIRECTIONS").Replace("\\n", "\n"), this.GetResourceManger().GetString("AUTOMATIC_DISASSEMBLY_MESSAGEBOX_CAPTION"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
      CApDisAutomaticDisassemblyDlgData congigurationData = this.GetCongigurationData();
      DialogResult dialogResult = DialogResult.Cancel;
      try
      {
        dialogResult = this.m_manager.Generate(congigurationData, this.m_disassemblyRoot.Object, this.m_mode);
      }
      finally
      {
        if (dialogResult != DialogResult.Cancel)
          ((Form) this).Close();
      }
    }
  }

  private void m_btnClose_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void SaveDialogConfiguration(
    CApDisAutomaticDisassemblyDlgData configurationData)
  {
    string str = "PersistencyData";
    if (this.m_mode == CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.ShortestPath)
      str = "ShortestPathSequenceGenerationPersistencyData";
    TxFormSettings.StoreFormValue((Form) this, str, "DistanceMode", ((int) configurationData.DistanceMode).ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "DirectionType", ((int) configurationData.DirectionType).ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "DistanceValue", configurationData.DistanceValue.ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "OperationDistance", configurationData.OperationDuration.ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "OperationType", configurationData.OperationType);
    TxFormSettings.StoreFormValue((Form) this, str, "Precision", configurationData.Precision.ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "Clearance", configurationData.Clearance.ToString());
  }

  private CApDisAutomaticDisassemblyDlgData LoadDialogConfiguration()
  {
    CApDisAutomaticDisassemblyDlgData disassemblyDlgData = new CApDisAutomaticDisassemblyDlgData();
    string str1 = "PersistencyData";
    if (this.m_mode == CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.ShortestPath)
      str1 = "ShortestPathSequenceGenerationPersistencyData";
    object s1;
    TxFormSettings.RestoreFormValue((Form) this, str1, "DistanceMode", ref s1);
    object s2;
    TxFormSettings.RestoreFormValue((Form) this, str1, "DirectionType", ref s2);
    object obj1;
    TxFormSettings.RestoreFormValue((Form) this, str1, "DistanceValue", ref obj1);
    object obj2;
    TxFormSettings.RestoreFormValue((Form) this, str1, "OperationDistance", ref obj2);
    object obj3;
    TxFormSettings.RestoreFormValue((Form) this, str1, "OperationType", ref obj3);
    object s3;
    TxFormSettings.RestoreFormValue((Form) this, str1, "Precision", ref s3);
    object s4;
    TxFormSettings.RestoreFormValue((Form) this, str1, "Clearance", ref s4);
    if (s1 != null && s2 != null && obj1 != null && obj2 != null && obj3 != null)
    {
      int num1 = int.Parse(s1 as string);
      int num2 = int.Parse(s2 as string);
      string s5 = obj1 as string;
      string s6 = obj2 as string;
      string str2 = obj3 as string;
      disassemblyDlgData.DistanceMode = (CApDisAutomaticDisassemblyDlgData.DisassemblyDistanceMode) num1;
      disassemblyDlgData.DirectionType = (CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType) num2;
      disassemblyDlgData.DistanceValue = double.Parse(s5);
      disassemblyDlgData.OperationDuration = double.Parse(s6);
      disassemblyDlgData.OperationType = str2;
    }
    disassemblyDlgData.UseAutomaticPathPlanner = false;
    disassemblyDlgData.Precision = s3 == null ? 5.0 : double.Parse(s3 as string);
    disassemblyDlgData.Clearance = s4 == null ? 2.0 : double.Parse(s4 as string);
    return disassemblyDlgData;
  }

  private CApDisAutomaticDisassemblyDlgData GetCongigurationData()
  {
    CApDisAutomaticDisassemblyDlgData congigurationData = new CApDisAutomaticDisassemblyDlgData();
    if (this.m_mode == CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.Disassembly)
    {
      if (this.m_cbAssembly.Checked && this.m_cbDisassembly.Checked)
        congigurationData.DirectionType = CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.AssemblyDisassembly;
      else if (this.m_cbDisassembly.Checked)
        congigurationData.DirectionType = CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.Disassembly;
    }
    else if (this.m_cbAssembly.Checked && this.m_cbDisassembly.Checked)
      congigurationData.DirectionType = CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.AssemblyShortestPathSequence;
    else if (this.m_cbDisassembly.Checked)
      congigurationData.DirectionType = CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.DisassemblyShortestPathSequence;
    if (this.m_rbAutomatic.Checked)
      congigurationData.DistanceMode = CApDisAutomaticDisassemblyDlgData.DisassemblyDistanceMode.Automatic;
    else if (this.m_rbExact.Checked)
      congigurationData.DistanceMode = CApDisAutomaticDisassemblyDlgData.DisassemblyDistanceMode.Exact;
    congigurationData.DistanceValue = Convert.ToDouble(this.m_nudExactValue.Value);
    congigurationData.OperationDuration = Convert.ToDouble(this.m_nudOperationDuration.Value);
    congigurationData.OperationType = this.m_cbOperationType.SelectedItem as string;
    congigurationData.UseAutomaticPathPlanner = false;
    return congigurationData;
  }

  private void SetConfigurationData(CApDisAutomaticDisassemblyDlgData dialogData)
  {
    if (dialogData.DirectionType == CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.AssemblyDisassembly || dialogData.DirectionType == CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.AssemblyShortestPathSequence)
      this.m_cbAssembly.Checked = true;
    else if (dialogData.DirectionType == CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.Disassembly || dialogData.DirectionType == CApDisAutomaticDisassemblyDlgData.DisassemblyDirectionType.DisassemblyShortestPathSequence)
      this.m_cbDisassembly.Checked = true;
    if (dialogData.DistanceMode == CApDisAutomaticDisassemblyDlgData.DisassemblyDistanceMode.Automatic)
    {
      this.m_rbAutomatic.Checked = true;
      this.m_nudExactValue.Enabled = false;
    }
    else if (dialogData.DistanceMode == CApDisAutomaticDisassemblyDlgData.DisassemblyDistanceMode.Exact)
      this.m_rbExact.Checked = true;
    this.m_nudExactValue.Value = Convert.ToDecimal(dialogData.DistanceValue);
    this.m_nudOperationDuration.Value = Convert.ToDouble(dialogData.OperationDuration);
    if (dialogData.OperationType != "")
      this.m_cbOperationType.SelectedItem = (object) dialogData.OperationType;
    if (this.m_cbOperationType.SelectedIndex != -1)
      return;
    this.m_cbOperationType.SelectedIndex = 0;
  }

  private void UpdateButtons()
  {
    if (this.m_disassemblyRoot.Object == null || this.m_cbOperationType.SelectedItem == null || !this.m_cbAssembly.Checked && !this.m_cbDisassembly.Checked)
      this.m_btnGenerate.Enabled = false;
    else
      this.m_btnGenerate.Enabled = true;
  }

  private void CUiDisAutomaticDisassemblyForm_Closing(object sender, CancelEventArgs e)
  {
    this.m_disassemblyRoot.UnInitialize();
    TxApplication.ApplicationEvents.Exiting -= new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
    this.SaveDialogConfiguration(this.GetCongigurationData());
  }

  private void ApplicationEvents_Exiting(object sender, TxApplication_ExitingEventArgs args)
  {
    ((Form) this).Close();
  }

  private void m_disassemblyRoot_Picked(object obj, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (this.m_disassemblyRoot.Object == null || this.m_disassemblyRoot.Object != args.Object)
      this.m_disassemblyRoot.ShowErrorWindow();
    else
      this.m_disassemblyRoot.HideErrorWindow();
    this.UpdateButtons();
  }

  private void m_disassemblyRoot_LostFocus(object obj, EventArgs args)
  {
    this.m_disassemblyRoot.HideErrorWindow();
  }

  private void ChangeTitlesToShoretstPathSequenceCommand()
  {
    ResourceManager resourceManger = this.GetResourceManger();
    this.m_partLable.Text = resourceManger.GetString("PART_TO_BE_REACHED");
    ((Control) this).Text = resourceManger.GetString("SHORTEST_PATH_SEQUENCE_CMD_NAME");
  }

  private void m_cbActivityType_SelectedIndexChanged(object sender, EventArgs e)
  {
    this.UpdateButtons();
  }

  private void m_disassemblyRoot_TypeInvalid(object sender, EventArgs e)
  {
    if (this.m_disassemblyRoot.Object == null)
      this.m_disassemblyRoot.ShowErrorWindow();
    this.UpdateButtons();
  }

  private void m_disassemblyRoot_TypeValid(object sender, EventArgs e)
  {
    if (this.m_disassemblyRoot.Object != null)
      this.m_disassemblyRoot.HideErrorWindow();
    this.UpdateButtons();
  }

  public enum SequenceGenerationMode
  {
    Disassembly,
    ShortestPath,
  }
}
